angular.module('personal', [])
    .controller('personalCtrl', ['$scope', 'httpService', '$localStorage', 'upload', '$rootScope', 'alertmsg', '$state', '$stateParams', function($scope, httpService, $localStorage, upload, $rootScope, alertmsg, $state, $stateParams) {
        if (!$localStorage.user && !$localStorage.mutian_token) {
            $state.go('login');
        }
        $scope.tab_type = 1;
        $scope.setTypePersonal = function(type) {
            $scope.tab_type = type;
        };

        if($stateParams.params == 3) {
            $scope.tab_type = 3;
        }

        $scope.paydata = {
            channel_type: 'bank'
        };

        $scope.currentUser = angular.copy($rootScope.user);

        console.log('$scope.currentUser', $scope.currentUser)
        if($scope.currentUser) {
            angular.forEach($scope.currentUser.deposit_accounts, function(v, k) {
                if(v.channel_type == 'bank') {
                    $scope.paydata = v;
                }
            });
        }


        $(function() {
            function changePreImg(el, preImg) {
                var file = el.target.files || el.dataTransfer.files;
                var fReader = new FileReader();
                fReader.readAsDataURL(file[0]);
                fReader.onloadend = function(event) {
                    var img = document.getElementById(preImg);
                    img.src = event.target.result;
                }
            }
            $('#pic').change(function(e) {
                changePreImg(e, 'img');
            });
            $('#pic-company-logo').change(function(e) {
                changePreImg(e, 'img-company-logo');
            });
            // var file = e.target.files || e.dataTransfer.files;
            // var fReader = new FileReader();
            // fReader.readAsDataURL(file[0]);
            // fReader.onloadend = function(event) {
            //     var img = document.getElementById("img");
            //     img.src = event.target.result;
            // }
        });

        var company_id;
        // 获取渠道列表
        function getCanals(company_id) {
            httpService.query('web/fetch_regions', { company_id: company_id, limit: 20 }).then(function(res) {
                if (res.code == 200) {
                    $scope.canals = res.regions;
                }
            });
        }

        if ($localStorage.user) {
            company_id = $localStorage.user.company_id;
            getCanals(company_id);
        }

        $scope.isbrank = false;
        $scope.savebrank = function(bool, data) {
            console.log('bool, data', bool, data)
            if(!bool) {
                var url = 'web/users/cash_account'
                // if(data.id) {
                //     url = 'web/users/edit_account';
                // }
                httpService.query(url, data, 'post').then(function(res) {
                    if(res.code == 200) {
                        getPersion($scope.currentUser.id);
                        alertmsg('保存成功！');
                    } else {
                        alertmsg(res.msg);
                    }
                });
            } else {
                $scope.isbrank = true;
            }
                
        };

        function getPersion(id) {
            if (id) {
                httpService.query('web/users/' + id).then(function(res) {
                    if (res.code == 200) {
                        $localStorage.user = res.user;
                        user = res.user;
                        $rootScope.user = res.user;
                        if(user && user.company_id) {
                            $scope.getRoles(user.company_id);
                        }
                        
                    }
                });
            } else {
                $state.go('login');
            }
        };

        // 修改个人资料
        $scope.ischangeuserform = false;
        $scope.putUser = function(bool, user) {
            if (!bool) {
                var file = document.querySelector('#pic').files[0];
                if (document.querySelector('#pic-company-logo')) {
                    var file1 = document.querySelector('#pic-company-logo').files[0];
                }

                var newUser = {
                    id: user.id,
                    real_name: user.real_name,
                    region: user.region,
                    brand_name: user.brand_name,
                }

                var formatData;
                if (file) {
                    newUser.isLogo = false;
                    newUser.headimg = user.headimg;
                    formatData = upload.format(file, newUser);
                } else if (file1) {
                    newUser.isLogo = true;
                    newUser.company_logo = user.company_logo;
                    formatData = upload.format(file1, newUser);
                } else {
                    formatData = upload.format(null, user);
                }

                httpService.query('web/users/' + user.id, formatData, 'put', true).then(function(res) {
                    if (res.code == 200) {
                        alertmsg('保存成功！');
                        $localStorage.user = $scope.currentUser = $rootScope.user = res.user;
                    } else {
                        alertmsg(res.msg);
                    }
                });

                // var formatData;
                // if (file) {
                //     user.isLogo = false;
                //     formatData = upload.format(file, user);
                // } else if (file1) {
                //     user.isLogo = true;
                //     formatData = upload.format(file1, user);
                // } else {
                //     formatData = upload.format(null, user);
                // }
                // httpService.query('web/users/' + user.id, upload.format(file, user), 'put', true).then(function(res) {
                //     if (res.code == 200) {
                //         alertmsg('保存成功！');
                //         $localStorage.user = $scope.currentUser = $rootScope.user = res.user;
                //     } else {
                //         alertmsg(res.msg);
                //     }
                // });
            } else {
                $scope.ischangeuserform = true;
            }
        };

    }]);
